package com.jsmy.marketing.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.VersionBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.CacheDataManager;
import com.jsmy.marketing.utils.DialogUtils;
import com.jsmy.marketing.utils.ToastUtil;
import com.jsmy.marketing.utils.VersionUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    VersionBean.DataBean bean;

    @Override
    protected int resId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.this.finish();
            }
        });
        tvVersion.setText(VersionUtil.getVersionName(this));

//        tvCache.setText(CacheDataManager.getTotalCacheSize(this));
        CacheDataManager.getTotalCacheSize(this).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                tvCache.setText(s);
            }
        });
        requestVersionInfo();
    }
//    版本信息
    private void requestVersionInfo() {
        Map<String,Object> map = new HashMap<>();
        map.put("pvertype","android");
        mPresenter.loadData(RetrofitManager.getInstance().getVersionInfo(map), Link.APP_VERSION_INFO);
    }

    @OnClick({R.id.btn_msg, R.id.btn_about, R.id.btn_users, R.id.btn_psd, R.id.btn_cahe, R.id.btn_version, R.id.btn_logout})
    public void onViewClicked(View view) {
        Bundle b = new Bundle();
        switch (view.getId()) {
            case R.id.btn_msg:
                b.putString("title","意见反馈");
                openActivity(WebActivity.class,b);
                break;
            case R.id.btn_about:
                b.putString("title","关于我们");
                openActivity(WebActivity.class,b);
                break;
            case R.id.btn_users:
                b.putString("title","用户协议");
                openActivity(WebActivity.class,b);
                break;
            case R.id.btn_psd:
                b.putString("title","修改密码");
                openActivity(WebActivity.class,b);
                break;
            case R.id.btn_cahe:
                DialogUtils.showCacheDialog(this, tvCache.getText().toString(), tvCache);
                break;
            case R.id.btn_version:
                if (VersionUtil.getVersionName(this).equals(bean.getNew_version())) {
                    ToastUtil.showShort(this, "当前已是最新版本！");
                } else {
                    DialogUtils.showVersionDialog(this, bean.getNew_version(), bean.getUpdate_log(), bean.getApk_file_url());
                }
                break;
            case R.id.btn_logout:
                DialogUtils.showLogBackDialog(this);
                break;
        }
    }
    @Override
    public void refreshData(Object response, String position) {
        switch (position) {
            case Link.APP_VERSION_INFO:
                bean = ((VersionBean) response).getData();
                break;
        }
    }
}
